package com.elite.groovy.dev;

import com.elite.groovy.kh.KHUtil;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.List;
import java.util.Map;

public class DevAvayaDataDeal {
    private static final Log log = LogFactory.getLog(DevAvayaDataDeal.class);
    private static JdbcTemplate jt = TxConfigUAT.getJdbcTemplate();
    private static JdbcTemplate a_jt = TxConfigAvaya.getJdbcTemplate();

    public static void main(String[] args) throws Exception {

        // 排班数据
        /*
        delete from Staff;
        delete from t_Pb_Bclist;
        delete from t_Pb_Staffresult;
        */
        VIEW_CLASSES_TABLE(2000);
    }


    // 3-排班数据
    public static String VIEW_CLASSES_TABLE(int Limit_Num) {
        String op_result = "";
        op_result = op_result + VIEW_CLASSES_TABLE_t_Pb_Staffresult(Limit_Num);
        return op_result;

    }

    // 3-3-排班数据-t_Pb_Staffresult
    public static String VIEW_CLASSES_TABLE_t_Pb_Staffresult(int Limit_Num) {
        String op_result = "";

        String tmp_Templist_Guid = KHUtil.getRandomString(36);

        String get_Data_SQL = "Select Mainid, Keyid, Staff_Id, To_Char(Workday, 'yyyy-mm-dd hh24:mi:ss') As Workday, Bclistid, Freeflag, Specialid, '' as Templist_Guid, Overtimeid, Overtimeflag From t_Pb_Staffresult  Where Workday >= Sysdate - 3 Order By Workday Desc";

        // 执行调用
        List<Map<String, Object>> res_Data_List = jt.queryForList(get_Data_SQL);
        log.debug("3-排班数据-t_Pb_Staffresult toDoGet Data:" + res_Data_List.size());

        int insert_Num = 0;
        int update_Num = 0;
        int fail_Num = 0;
        for (int i = 0; i < res_Data_List.size(); i++) {
            log.debug("3-排班数据-t_Pb_Staffresult res_Data_List Data:" + res_Data_List.get(i).toString());
            op_result = res_Data_List.get(i).toString();
            String Keyid = "";
            try {

                Keyid = KHUtil.GetMapV(res_Data_List.get(i), "Keyid");
                String Mainid = KHUtil.GetMapV(res_Data_List.get(i), "Mainid");
                String Staff_Id = KHUtil.GetMapV(res_Data_List.get(i), "Staff_Id");
                String Workday = KHUtil.GetMapV(res_Data_List.get(i), "Workday");
                String Bclistid = KHUtil.GetMapV(res_Data_List.get(i), "Bclistid");
                String Freeflag = KHUtil.GetMapV(res_Data_List.get(i), "Freeflag");
                String Specialid = KHUtil.GetMapV(res_Data_List.get(i), "Specialid");
                String Overtimeid = KHUtil.GetMapV(res_Data_List.get(i), "Overtimeid");
                String Overtimeflag = KHUtil.GetMapV(res_Data_List.get(i), "Overtimeflag");

                // 检查当前数据库是否存在 存在更新 不存在新建
                String sql = "SELECT COUNT(*) AS COUNT FROM t_Pb_Staffresult WHERE Keyid = ?";
                List<Map<String, Object>> sqlResult = a_jt.queryForList(sql, Keyid);
                int Countnum = Integer.parseInt(sqlResult.get(0).get("COUNT").toString());

                if (Countnum <= 0) {

                    sql = "Insert Into t_Pb_Staffresult (Mainid, Keyid, Staff_Id, Workday, Bclistid, Freeflag, Specialid, Overtimeid, Overtimeflag, Templist_Guid) Values ('" + Mainid + "', '" + Keyid + "', '" + Staff_Id + "', To_Date('" + Workday + "', 'yyyy-mm-dd hh24:mi:ss'), '" + Bclistid + "', '" + Freeflag + "', '" + Specialid + "', '" + Overtimeid + "', '" + Overtimeflag + "', '" + tmp_Templist_Guid + "')";
                    log.debug("3-排班数据-t_Pb_Staffresult DB SQL:" + sql);

                    a_jt.update(sql);
                    insert_Num++;

                } else {
                    sql = "Update t_Pb_Staffresult Set Mainid = '" + Mainid + "', Staff_Id = '" + Staff_Id + "', Workday = To_Date('" + Workday + "', 'yyyy-mm-dd hh24:mi:ss'), Bclistid = '" + Bclistid + "', Freeflag = '" + Freeflag + "', Specialid = '" + Specialid + "', Overtimeid = '" + Overtimeid + "', Overtimeflag = '" + Overtimeflag + "', Templist_Guid = '" + tmp_Templist_Guid + "' Where Keyid = '" + Keyid + "'";
                    log.debug("3-排班数据-t_Pb_Staffresult DB SQL:" + sql);

                    a_jt.update(sql);
                    update_Num++;
                }

            } catch (Exception e) {
                fail_Num++;
                op_result = "t_Pb_Staffresult:" + Keyid + " 处理异常:" + e.getMessage();
                log.warn(op_result, e);
            }
        }

        // 将本次没有更新到的数据删除
        int del_Num = a_jt.update("Delete From t_Pb_Staffresult  Where Workday >= Sysdate - 3 And Templist_Guid != ?", tmp_Templist_Guid);
        log.debug("3-排班数据-将本次没有更新到的数据删除t_Pb_Staffresult DB Num:" + del_Num);

        op_result = op_result + "3-排班数据-t_Pb_Staffresult 回写完成 总数:" + res_Data_List.size() + " 新建:" + insert_Num + " 更新:" + update_Num + " 失败:" + fail_Num + " 删除:" + del_Num;
        log.debug(op_result);

        return op_result;

    }

}
